package com.sniper.web.action;

import com.sniper.utils.Verify;
import com.sniper.utils.VerifyCode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Date;

/**
 * 提供图片验证码
 *
 * @author laolang
 */
@Controller
public class VerifyController {

    public static final String VALIDATE_CODE;

    static {
        VALIDATE_CODE = "sessionVerifyName";
    }

    public VerifyController() {

    }


    @ResponseBody
    @RequestMapping("verify")
    public void verify(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必须放在第一行才管用
        Date date = new Date();
        // http://hussion.iteye.com/blog/1456702
        // set 意识永远用新的值，add旧的存在就用旧的
        response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);
        response.setDateHeader("Expires", date.getTime());
        response.setDateHeader("Last-Modified", date.getTime());
        response.setHeader("Server", "sfe/1.0.0.00");
        response.setStatus(HttpServletResponse.SC_OK);
        // 如果开启Hard模式，可以不区分大小写
        String securityCode = VerifyCode.getSecurityCode(4,
                VerifyCode.SecurityCodeLevel.Hard, false).toLowerCase();
        // 获取默认难度和长度的验证码
        // String securityCode = VerifyCode.getSecurityCode();
        // 图片流
        OutputStream outputStream = response.getOutputStream();

        BufferedImage bufferedImage = Verify.createImage(securityCode);
        ImageIO.write(bufferedImage, "png", outputStream);
        response.setIntHeader("Content-Length", response.getBufferSize());
        // 放入session中
        request.getSession(false).setAttribute(VALIDATE_CODE, securityCode);

        outputStream.flush();
        outputStream.close();

    }


    @ResponseBody
    @RequestMapping("verify2")
    public ResponseEntity<byte[]> verify2(HttpServletResponse response, HttpServletRequest request)
            throws Exception {
        // 如果开启Hard模式，可以不区分大小写
        String securityCode = VerifyCode.getSecurityCode(5,
                VerifyCode.SecurityCodeLevel.Hard, false).toLowerCase();
        // 获取默认难度和长度的验证码
        // String securityCode = VerifyCode.getSecurityCode();
        // 图片流
        ServletOutputStream outputStream = response.getOutputStream();
        BufferedImage bufferedImage = Verify.createImage(securityCode);
        ImageIO.write(bufferedImage, "png", outputStream);
        // 放入session中
        request.getSession().setAttribute(VALIDATE_CODE, securityCode);
        byte[] body = new byte[response.getBufferSize()];
        outputStream.write(body);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "image/png");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        HttpStatus httpState = HttpStatus.OK;
        return new ResponseEntity<>(body, headers, httpState);
    }


}